<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/22  16:46
// +----------------------------------------------------------------------
namespace Admin\Controller;
class BuyController extends CashierBaseController {

    function index() {
        $this->display();
    }

    function doBuy() {
        $data = I("post.");
        $blist = explode("," , $data['liststr']);
        $blist = array_count_values($blist);
        $summary = '线下购书,购书清单：';
        foreach ($blist as $k => $v) {
            $summary .= "ID:{$k},{$v}本;";
        }
        $summary .= "会员折扣为:" . $data['discount_buy'] . ",";
        $summary .= "原总金额为:" . $data['money_b'] . ",";
        $summary .= "打折后总金额为:" . $data['money'];
        //生成支付订单pay
        $Pay = M("Pay");
        $Pay->startTrans();
        $map_pay = [
            "user_id" => $data['user_id'] ,
            "status" => 1 ,
            "type" => 2 , 
            "way" => $data['pay_way'] ,
            "summary" => $summary , 
            "money" => $data['money'] ,
            "money_real" => $data['money_real'] ,
            "money_zero" => $data['money_zero']
        ];
        $pay_id = $Pay->data($map_pay)->add();
        if (!$pay_id) {
            $Pay->rollback();
            $this->error("支付失败");
        }
        //生成订单order
        $Order = M("Order_book");
        $map_order = ["user_id" => $data['user_id'] , "money" => $data['money_b'] , "price_freight" => 0 , "type" => 0 , "address" => "{'info':'线下购买'}" , "state_order" => "3" , "pay_id" => $pay_id , "discount_buy" => $data['discount_buy'],'is_online'=>'1'];
        $order_id = $Order->data($map_order)->add();
        if (!$order_id) {
            $Order->rollback();
            $this->error("支付失败");
        }
        //生成订单图书数据
        $Orderbook = M("Order_has_book");
        foreach ($blist as $k => $v) {
            $map_book[] = ["order_id" => $order_id , "book_id" => $k , "quantity" => $v , "type" => 0];
            $rr=M('Book')->where(['book_id'=>$k])->setDec('stock_buy',$v);//减速库存
            if(!$rr){
                $Orderbook->rollback();
                $this->error("库存不足");
            }
        }
        $r = $Orderbook->addAll($map_book);

        if ($r) {
            $Orderbook->commit();
            $this->success("支付成功" , U("orderShow" , "order_id=" . $order_id));
        } else {
            $Orderbook->rollback();
            $this->error("支付失败");
        }

    }

    function orderShow() {
        $order_id = I("get.order_id");
        $d = [];
        $da = M("Order_book")->where(["order_id" => $order_id])->find();
        $dp = M("Pay")->where(["pay_id" => $da['pay_id']])->find();

        $d['order_id'] = $da['order_id']; //订单号
        $d['pay_id'] = $da['pay_id']; //支付流水号
        $d['money_b'] = $da['money']; //原价
        $d['type'] = $da['type'] == 0 ? "购书" : "借阅";
        $d['discount_buy'] = $da['discount_buy']; //折扣
        $d['money'] = $dp['money']; //实际价格
        $d['money_real'] = $dp['money_real']; //实收
        $d['money_zero'] = $dp['money_zero']; //找零
        $d['create_time'] = $dp['create_time']; //找零
        $dc = M("View_order_has_book")->where(["order_id" => $order_id])->field("sm,dj,quantity")->select();
        $d['list'] = $dc;

        $user_id=$dp['user_id']==''?0:$dp['user_id'];
        $User= new \Common\Model\UserModel();
        $d['card']=$User->getCard($user_id);
        $d['score']=$User->getScore($user_id);

        $this->assign("d" , $d);
        $this->display('Print:orderBuy');
    }

    function buyCheck(){
        $blist = explode("," , I("post.liststr"));
        $blist = array_count_values($blist);
        foreach($blist as $k=>$v){
//            $c=M("Book")->where(["book_id"=>$k,"stock_buy"=>['egt',$v]])->count();
            $c=M("Book")->where(["book_id"=>$k])->count();  //支持负库存
            if($c==0){
                $r=[
                    "status"=>"error",
                    "data"=>"库存不足"
                ];
                echo json_encode($r);
                return;
            }
        }
        $r=[
            "status"=>"success",
            "data"=>"可购买"
        ];
        echo json_encode($r);
        return;
    }
}